package com.example.unitestexample.unittests.normaltests;

import org.junit.jupiter.api.*;

import java.util.concurrent.TimeUnit;

/**
 * 实例展示普通的方法/类如何测试，此处使用的是JUnit 5.
 *
 * 里面每一个测试用例的注解 @Test用的是 org.junit.jupiter.api.Test
 * yong
 */
public class SimpleTestsWithoutContext {

    @BeforeAll
    public static void beforeAll() {
        System.out.println("called before all");
    }

    @AfterAll
    public static void afterAll() {
        System.out.println("called after all");
    }

    @BeforeEach
    public void beforeEach() {
        System.out.println("called before each");
    }

    @AfterEach
    public void afterEach() {
        System.out.println("Called after each.");
    }

    /**
     * 如果用的是Junit4中的org.junit.Test注解，就不能与Junit5中的@BeforeEach/@BeforeAll
     * 配合使用了。
     */
    @org.junit.Test
    public void intTest() {
        System.out.println("Testing intTest");
        int num = 1;
        Assertions.assertEquals(1, num);
        System.out.println("intTest passed");
    }

    @Test
    public void stringTest() {
        System.out.println("Testing stringTest");
        String str = "Hi";
        Assertions.assertEquals("Hi", str);
        System.out.println("stringTest passed.");
    }

    /**
     * `@Ignore`在JUnit5中不存在，换成了`@Disabled`。
     * 这种场景在编码规范里面实际上是不建议使用的，应该是直接删除这种测试用例
     */
    @Disabled
    @Test
    public void ignoredTest() {
        Assertions.fail("You should not see me, I am an ignored test case.");
    }

    @Timeout(value = 5, unit = TimeUnit.SECONDS)
    @Test
    public void timeoutTest() throws InterruptedException {
        Thread.sleep(6 * 1000);
        Assertions.fail("You should not see me.");
    }
}
